define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'template/order/index' + location.search,
                    add_url: 'template/order/add',
                    edit_url: 'template/order/edit',
                    del_url: 'template/order/del',
                    multi_url: 'template/order/multi',
                    import_url: 'template/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'order_sn', title: __('Order_sn'), operate: false},
                        {
                            field: 'user_id',
                            title: __('Username'),
                            searchList: Config.userNameList,
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'service_fee', title: __('Service_fee'), operate: false},
                        {
                            field: 'pay_state',
                            title: __('Pay_state'),
                            formatter: this.api.formatter.status,
                            searchList: {
                                0: "待付款",
                                1: "已完成",
                                2: "已取消",
                            }
                        },
                        {
                            field: 'paytime',
                            title: __('Paytime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {1: "核算模板", 2: "核算服务"}
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate, buttons: [{
                                name: 'detail',
                                text: __('订单详情'),
                                icon: 'fa fa-list',
                                extend: 'data-area=["70%","60%"]',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'template/order/detail?ids={ids}'
                            },
                                {
                                    name: 'edit',
                                    hidden: true // 隐藏修改按钮
                                },
                                {
                                    name: 'del',
                                    hidden: true // 隐藏删除按钮
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                status: function (value, row, index) {
                    switch (value) {
                        case '已完成':
                            return '<span class="label label-success">已完成</span>';
                        case '已取消':
                            return '<span class="label label-danger">已取消</span>';
                        case '待付款':
                            return '<span class="label label-primary">待付款</span>';
                        default:
                            return value;
                    }
                }
            },

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
